package com.chinatechstar.base.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.chinatechstar.base.dto.SysWellTrackDTO;
import com.chinatechstar.base.dto.UploadDailyOilProdDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.chinatechstar.base.entity.SysWell;
import com.chinatechstar.base.service.SysWellService;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;
import org.springframework.web.multipart.MultipartFile;

/**
 * 基础数据-井 的控制层
 */
@Api(value = "基础数据-井 的控制层", tags = {"基础数据-井"})
@RestController
@RequestMapping("/syswell")
public class SysWellController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysWellService sysWellService;

    /**
     * 查询 基础数据-井 所有数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 基础数据-井 所有数据", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellAll")
    public ListResult<Object> findSysWellAll() {
        List<SysWell> list = sysWellService.findSysWellAll();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询 基础数据-井 不分页
     *
     * @param wellOrgId      生产单位ID
     * @param wellAreaId     区块ID
     * @param wellCommonName 井通用名称
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 基础数据-井 不分页", tags = {"基础数据-井"})
    @GetMapping(path = "/findWellListByCondition")
    public ListResult<Object> findWellListByCondition(@RequestParam(name = "wellOrgId", required = false) Long wellOrgId,
                                                      @RequestParam(name = "wellAreaId", required = false) Long wellAreaId,
                                                      @RequestParam(name = "wellCommonName", required = false) String wellCommonName) {
        List<SysWell> data = sysWellService.findWellListByCondition(wellOrgId, wellAreaId, wellCommonName);
        return ResultBuilder.buildListSuccess(data);
    }
    /**
     * 查询 井区块 不分页
     *
     * @param wellAreaName 井区域名
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 井区块 不分页", tags = {"井区块"})
    @GetMapping(path = "/findWellAreaNameByCondition")
    public ListResult<Object> findWellAreaNameByCondition(@RequestParam(name = "wellAreaName", required = false) String wellAreaName) {
        List<SysWell> data = sysWellService.findWellAreaNameByCondition(wellAreaName);
        return ResultBuilder.buildListSuccess(data);
    }


    /**
     * 查询 基础数据-井 分页
     *
     * @param currentPage    当前页数
     * @param pageSize       每页记录数
     * @param wellOrgId      生产单位ID
     * @param wellAreaId     区块ID
     * @param wellCommonName 井通用名称
     * @param sorter         排序
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 基础数据-井 分页", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellByCondition")
    public ListResult<Object> findSysWellByCondition(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                     @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam(name = "wellOrgId", required = false) Long wellOrgId,
                                                     @RequestParam(name = "wellAreaId", required = false) Long wellAreaId,
                                                     @RequestParam(name = "wellCommonName", required = false) String wellCommonName,
                                                     @RequestParam(name = "sorter", required = false) String sorter) {
        Map<String, Object> data = sysWellService.findSysWellByCondition(currentPage, pageSize, wellOrgId, wellAreaId, wellCommonName, sorter);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 根据查询条件导出 基础数据-井
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/exportSysWell")
    public void exportSysWell(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "名称", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysWellService.findSysWellForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据主键ID查询 基础数据-井 数据
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数据查询 基础数据-井 数据", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellById")
    public ActionResult findSysWellById(@RequestParam(name = "id") Long id) {
        SysWell data = sysWellService.findSysWellById(id);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 根据主键ID数组查询 基础数据-井 数据
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组查询 基础数据-井 数据", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellByIds")
    public ListResult<Object> findSysWellByIds(@RequestParam(name = "ids") Long[] ids) {
        List<SysWell> list = sysWellService.findSysWellByIds(ids);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 根据时间查询CO2吞吐效果跟踪数据 基础数据-井
     *
     * @param date 日期
     * @param dict 数据字典
     */
    @ApiOperation(value = "根据时间查询CO2吞吐效果跟踪数据 基础数据-井", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellTrackByDateAndGasMedium")
    public ListResult<Object> findSysWellTrackByDateAndGasMedium(@RequestParam(name = "date") String date,
                                                                 @RequestParam(name = "dict") String dict) {
        List<LinkedHashMap<String, Object>> data = sysWellService.findSysWellTrackByDateAndGasMedium(date, dict);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询所有注气吞吐实施的井
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询所有注气吞吐实施的井", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellWithAllImpl")
    public ListResult<Object> findSysWellWithAllImpl() {
        List<SysWell> list = sysWellService.findSysWellWithAllImpl();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询目前正在进行注气吞吐实施的井
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询目前正在进行注气吞吐实施的井", tags = {"基础数据-井"})
    @GetMapping(path = "/findSysWellWithImpl")
    public ListResult<Object> findSysWellWithImpl() {
        List<SysWell> list = sysWellService.findSysWellWithImpl();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 新增 基础数据-井
     *
     * @param sysWell 基础数据-井 实体
     * @return ActionResult
     */
    @ApiOperation(value = "新增 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/addSysWell")
    public ActionResult addSysWell(@Validated(InsertValidator.class) @RequestBody SysWell sysWell) {
        sysWellService.insertSysWell(sysWell);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量新增 基础数据-井
     *
     * @param sysWells 基础数据-井 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量新增 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/addSysWells")
    public ActionResult addSysWells(@Validated(InsertValidator.class) @RequestBody List<SysWell> sysWells) {
        sysWellService.insertSysWells(sysWells);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑 基础数据-井
     *
     * @param sysWell 基础数据-井 实体
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 基础数据-井", tags = {"基础数据-井"})
    @PutMapping(path = "/updateSysWell")
    public ActionResult updateSysWell(@Validated(UpdateValidator.class) @RequestBody SysWell sysWell) {
        sysWellService.updateSysWell(sysWell);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量编辑 基础数据-井
     *
     * @param sysWells 基础数据-井 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量编辑 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/updateSysWells")
    public ActionResult updateSysWells(@Validated(UpdateValidator.class) @RequestBody List<SysWell> sysWells) {
        sysWellService.updateSysWells(sysWells);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑油井产状数据
     *
     * @param dto 油井产状 dto
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/updateOilProdTrack")
    public ActionResult updateOilProdTrack(@Validated @RequestBody SysWellTrackDTO dto) {
        sysWellService.updateOilProdTrack(dto);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 基础数据-井
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID删除 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/deleteSysWellById")
    public ActionResult deleteSysWellById(@RequestParam(name = "id") Long id) {
        sysWellService.deleteSysWellById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 基础数据-井
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量删除 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/deleteSysWellByIds")
    public ActionResult deleteSysWellByIds(@RequestParam(name = "id") Long[] ids) {
        sysWellService.deleteSysWellByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 基础数据-井
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID软删除 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/softDeleteSysWellById")
    public ActionResult softDeleteSysWellById(@RequestParam(name = "id") Long id) {
        sysWellService.softDeleteSysWellById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 基础数据-井
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量软删除 基础数据-井", tags = {"基础数据-井"})
    @PostMapping(path = "/softDeleteSysWellByIds")
    public ActionResult softDeleteSysWellByIds(@RequestParam(name = "id") Long[] ids) {
        sysWellService.softDeleteSysWellByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }


    /**
     * 导入每日油井产状，并计算CO2吞吐效果
     *
     * @param dto 数据dto
     * @return ActionResult
     */
    @ApiOperation(value = "导入每日油井产状，并计算CO2吞吐效果", tags = {"基础数据-井"})
    @PostMapping(path = "/uploadDailyOilProd", consumes = {"multipart/form-data"})
    public ActionResult uploadDailyOilProd(@ModelAttribute @Valid UploadDailyOilProdDTO dto) throws IOException {
        List<String> data = sysWellService.uploadDailyOilProd(dto);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 导入CO2吞吐效果追踪表基础数据
     *
     * @param file 文件资源
     * @param dict 数据字典
     * @return ActionResult
     */
    @ApiOperation(value = "导入CO2吞吐效果追踪表基础数据", tags = {"基础数据-井"})
    @PostMapping(path = "/uploadCo2EffectTrack", consumes = {"multipart/form-data"})
    public ActionResult uploadCo2EffectTrack(@RequestParam(name = "file") MultipartFile file,
                                             @RequestParam(name = "dict") String dict) throws IOException {
        List<String> data = sysWellService.uploadCo2EffectTrack(file, dict);
        return ResultBuilder.buildActionSuccess(data);
    }


}
